package com.codi.api.validation;

import com.codi.api.security.UserUtil;
import com.codi.base.cache.CacheUtil;
import com.codi.base.config.ConfigurationMgr;
import com.codi.base.util.EqualsUtil;
import com.codi.base.util.StringUtil;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.constant.PayTypeConst;
import com.codi.bus.core.annotation.Validator;
import com.codi.bus.core.resultModel.BaseResult;
import com.codi.bus.core.vo.PortfolioSellLimitRequestVo;
import com.codi.bus.core.vo.PortfolioTradeBuyVo;
import com.codi.bus.core.vo.PortfolioTradeSellVo;
import com.codi.bus.core.vo.PortfolioTradeUndoVo;
import com.codi.message.constant.ErrorConst;
import com.codi.message.constant.ErrorConstant;
import com.codi.message.util.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.concurrent.TimeUnit;

/**
 * 组合交易校验
 *
 * @author spy
 * @version 1.0 2017-11-06 16:31
 * @since 1.0
 */
@Slf4j
@Validator
public class PortfolioValidation implements InitializingBean {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    // 交易申请编号， 用于防止重复提交交易
    private ValueOperations<String, String> applyCache;

    @Override
    public void afterPropertiesSet() throws Exception {
        // 申请编号
        applyCache = redisTemplate.opsForValue();
    }

    /**
     * 校验交易限制
     *
     * @param vo
     * @return
     */
    public BaseResult validateLimits(PortfolioSellLimitRequestVo vo) {
        BaseResult result = new BaseResult(false);

        String capitalMode = vo.getCaptialMode();

        if (EqualsUtil.equals(capitalMode.toUpperCase(), GlobalConstant.CAPITAL_MODE_EPAY)
            || EqualsUtil.equals(capitalMode.toUpperCase(), GlobalConstant.CAPTIAL_MODE_K)
            || EqualsUtil.equals(capitalMode.toUpperCase(), GlobalConstant.CAPTIAL_MODE_I)) {
        } else {
            log.error("capital={},参数不合法", capitalMode);
            result.setErrorCode(ErrorConst.COMMON_INVALID_PARAM);
            result.setErrorMessage(MessageUtil.getErrorMsg(ErrorConst.COMMON_INVALID_PARAM));
            return result;
        }

        result.setSuccess(true);
        return result;
    }

    /**
     * 校验交易限制
     *
     * @param portfolioCode
     * @param capitalMode
     * @param businCode
     * @return
     */
    public BaseResult validateLimits(String portfolioCode, String capitalMode, String businCode) {
        BaseResult result = new BaseResult(false);


        if (EqualsUtil.equals(capitalMode.toUpperCase(), GlobalConstant.CAPITAL_MODE_EPAY)
            || EqualsUtil.equals(capitalMode.toUpperCase(), GlobalConstant.CAPTIAL_MODE_K)
            || EqualsUtil.equals(capitalMode.toUpperCase(), GlobalConstant.CAPTIAL_MODE_I)) {
        } else {
            log.error("capital={},参数不合法", capitalMode);
            result.setErrorCode(ErrorConst.COMMON_INVALID_PARAM);
            result.setErrorMessage(MessageUtil.getErrorMsg(ErrorConst.COMMON_INVALID_PARAM));
            return result;
        }

        if (EqualsUtil.equals(businCode, GlobalConstant.FUND_BUSIN_CODE_APPLY_BUY)
            || EqualsUtil.equals(businCode, GlobalConstant.FUND_BUSIN_CODE_APPLY_SELL)) {
        } else {
            log.error("businCode={},参数不合法", businCode);
            result.setErrorCode(ErrorConst.COMMON_INVALID_PARAM);
            result.setErrorMessage(MessageUtil.getErrorMsg(ErrorConst.COMMON_INVALID_PARAM));
            return result;
        }

        result.setSuccess(true);
        return result;

    }

    /**
     * 校验申购
     *
     * @param buyVo
     * @return
     */
    public BaseResult validateBuy(PortfolioTradeBuyVo buyVo) {
        BaseResult result = new BaseResult(false);


        if (EqualsUtil.equals(buyVo.getPayType(), PayTypeConst.BANK)) {

        } else {
            result.setErrorCode(ErrorConst.COMMON_NOT_SUPPORT_PAY_TYPE);
            result.setErrorMessage(MessageUtil.getErrorMsg(ErrorConst.COMMON_NOT_SUPPORT_PAY_TYPE));
            return result;
        }

        // 验证是否重复提交, 5分钟过期
        String applyNo = buyVo.getApplyNo() + UserUtil.getLoginUserId();

        String applyKey = CacheUtil.getKey(GlobalConstant.REDIS_KEY_APPLY_BUY, applyNo);

        Boolean isDev = ConfigurationMgr.getInstance().getBoolean(GlobalConstant.CONFIG_IS_DEVELOPMENT, false);

        if (!isDev && !StringUtil.isEmpty(applyCache.get(applyKey))) {
            result.setErrorCode(ErrorConstant.ERROR_DUPLICATE_APPLY);
            result.setErrorMessage(MessageUtil.getErrorMsg(ErrorConstant.ERROR_DUPLICATE_APPLY));

            return result;
        }
        applyCache.set(applyKey, applyNo, GlobalConstant.APPLY_EXPIRED_HOURS_DEFAULT, TimeUnit.HOURS);

        result.setSuccess(true);
        return result;
    }

    /**
     * 校验赎回
     *
     * @param vo
     * @return
     */
    public BaseResult validateSell(PortfolioTradeSellVo vo) {
        BaseResult result = new BaseResult(false);
        String applyNo = vo.getApplyNo() + UserUtil.getLoginUserId();


        String applyKey = CacheUtil.getKey(GlobalConstant.REDIS_KEY_APPLY_SELL, applyNo);

        Boolean isDev = ConfigurationMgr.getInstance().getBoolean(GlobalConstant.CONFIG_IS_DEVELOPMENT, false);

        if (!isDev && !StringUtil.isEmpty(applyCache.get(applyKey))) {
            result.setErrorCode(ErrorConstant.ERROR_DUPLICATE_APPLY);
            result.setErrorMessage(MessageUtil.getErrorMessage(ErrorConstant.ERROR_DUPLICATE_APPLY, null));
            return result;
        }
        applyCache.set(applyKey, applyNo, GlobalConstant.APPLY_EXPIRED_HOURS_DEFAULT, TimeUnit.HOURS);

        result.setSuccess(true);
        return result;
    }


    /**
     * 校验撤单
     *
     * @param vo
     * @return
     */
    public BaseResult validateUndo(PortfolioTradeUndoVo vo) {
        BaseResult result = new BaseResult(false);
        String applyNo = vo.getApplyNo() + UserUtil.getLoginUserId();

        Boolean isDev = ConfigurationMgr.getInstance().getBoolean(GlobalConstant.CONFIG_IS_DEVELOPMENT, false);

        String applyKey = CacheUtil.getKey(GlobalConstant.REDIS_KEY_APPLY_UNDO, applyNo);
        if (!isDev && !StringUtil.isEmpty(applyCache.get(applyKey))) {
            result.setErrorCode(ErrorConstant.ERROR_DUPLICATE_APPLY);
            result.setErrorMessage(MessageUtil.getErrorMessage(ErrorConstant.ERROR_DUPLICATE_APPLY, null));
            return result;
        }
        applyCache.set(applyKey, applyNo, GlobalConstant.APPLY_EXPIRED_HOURS_DEFAULT, TimeUnit.HOURS);

        result.setSuccess(true);
        return result;
    }

}
